<?php
// Step 3 of web-cp setup
//   Server services and programs setup - e.g. Web Server, FTP Server, Mail Server

switch($cfg['os'].'/'.$cfg['osversion']) {
	case 'linux/RHEL':
	case'linux/FedoraCore':
	case'linux/RedHat':
	case'linux/Debian':
	case'linux/Suse':
	case'linux/Other':
	case'linux/Unknown':
		$data['init_dir'] = '/etc/init.d';
		$init_prefix = '';
		break;
	case'linux/Slackware':
		$data['init_dir'] = '/etc/rc.d';
		$init_prefix = 'rc.';
		break;
	case'freebsd/FreeBSD4':
	case 'freebsd/NetBSD':
		$data['init_dir'] = '/etc/rc.d';
		$init_prefix = '';
		break;
	case 'freebsd/OpenBSD':
		$data['init_dir'] = '/etc/rc.local';
		$init_prefix = '';
		break;
	case'solaris/10':
		$data['init_dir'] = '/etc/init.d';
		$init_prefix = '';
		break;
	case'mac/OSX':
		$data['init_dir'] = '/Library/StartupItems/';
		$init_prefix = '';
		break;
}

// Set daemon locations based on selected servers
//   If nothing chosen use default.

if ($data['httpserver'] != '' || $cfg['httpserver'] != '') {
	if ($data['httpserver'] == 'apache1' || $cfg['httpserver'] == 'apache1') { $data['init']['httpd'] = $cfg['init_dir'].'/'.$init_prefix.'httpd'; }
	if ($data['httpserver'] == 'apache2' || $cfg['httpserver'] == 'apache2') { $data['init']['httpd'] = $cfg['init_dir'].'/'.$init_prefix.'httpd'; }
} elseif ($data['httpserver'] != 'none') {
	$data['init']['httpd'] = $cfg['init_dir'].'/'.$init_prefix.'httpd';
}

if ($data['ftpserver'] != '' || $cfg['ftpserver'] != '') {
	if ($data['ftpserver'] == 'proftpd' || $cfg['ftpserver'] == 'proftpd') { $data['init']['proftpd'] = $cfg['init_dir'].'/'.$init_prefix.'proftpd'; }
	if ($data['ftpserver'] == 'pureftpd' || $cfg['ftpserver'] == 'pureftpd') { $data['init']['pureftpd'] = $cfg['init_dir'].'/'.$init_prefix.'pureftpd'; }
	if ($data['ftpserver'] == 'vsftpd' || $cfg['ftpserver'] == 'vsftpd') { $data['init']['vsftpd'] = $cfg['init_dir'].'/'.$init_prefix.'vsftpd'; }
	if ($data['ftpserver'] == 'wuftpd' || $cfg['ftpserver'] == 'wuftpd') { $data['init']['wuftpd'] = $cfg['init_dir'].'/'.$init_prefix.'wuftpd'; }
} elseif ($data['ftpserver'] != 'none') {
	$data['init']['proftpd'] = $cfg['init_dir'].'/'.$init_prefix.'proftpd';
}

if ($data['mailserver'] != '' || $cfg['mailserver'] != '') {
	if ($data['mailserver'] == 'exim4' || $cfg['mailserver'] == 'exim4') { $data['init']['exim'] = $cfg['init_dir'].'/'.$init_prefix.'exim4'; }
	if ($data['mailserver'] == 'sendmail' || $cfg['mailserver'] == 'sendmail') { $data['init']['sendmail'] = $cfg['init_dir'].'/'.$init_prefix.'sendmail'; }
	if ($data['mailserver'] == 'qmail' || $cfg['mailserver'] == 'qmail') { $data['init']['qmail'] = $cfg['init_dir'].'/'.$init_prefix.'qmail'; }
	if ($data['mailserver'] == 'virtualqmail' || $cfg['mailserver'] == 'virtualqmail') { $data['init']['vqmail'] = $cfg['init_dir'].'/'.$init_prefix.'vqmail'; }
} elseif ($data['mailserver'] != 'none') {
	$data['init']['sendmail'] = $cfg['init_dir'].'/'.$init_prefix.'sendmail';
}

if ($data['dns_system'] != '' || $cfg['dns_system'] != '') {
	if ($data['dns_system'] == 'bind' || $cfg['dns_system'] == 'bind') { $data['init']['bind'] = $cfg['init_dir'].'/'.$init_prefix.'bind'; }
	if ($data['dns_system'] == 'mydns' || $cfg['dns_system'] == 'mydns') { $data['init']['mydns'] = $cfg['init_dir'].'/'.$init_prefix.'mydns'; }
	if ($data['dns_system'] == 'pdns' || $cfg['dns_system'] == 'pdns') { $data['init']['pdns'] = $cfg['init_dir'].'/'.$init_prefix.'pdns'; }
	if ($data['dns_system'] == 'tinydns' || $cfg['dns_system'] == 'tinydns') { $data['init']['tinydns'] = $cfg['init_dir'].'/'.$init_prefix.'tinydns'; }
} elseif ($data['dns_system'] != 'none') {
	$data['init']['bind'] = $cfg['init_dir'].'/'.$init_prefix.'bind';
}

if ($data['maillist'] != '' || $cfg['maillist'] != '') {
	if ($data['mailserver'] == 'mailman' || $cfg['maillist'] == 'mailman') { $data['init']['mailman'] = $cfg['init_dir'].'/'.$init_prefix.'mailman'; }
} elseif ($data['maillist'] != 'none') {
	$data['init']['mailman'] = $cfg['init_dir'].'/'.$init_prefix.'mailman';
}

?>
<!--BEGIN HTML-->
<!--<input type=hidden name="step" value="3">-->
<input type=hidden name="data[<?=$step==3?'initscriptslocated':'programslocated';?>]" value="true">
<!-- PROGRAM & SERVICE LOCATION CONFIGURATION -->
<div align="center">
<table border="0" cellpadding="0" cellspacing="0">
<tr><td>
<?php if ($step == 3) { ?>
	<p align="justify">Please note PID files below are defaults only, you MUST<br>
change them to the correct values for your distro</p>
<?php } ?>
<table border="0" cellpadding="0" cellspacing="0" class="tblbg"><tr><td>
<table border="0" cellspacing="1" width="400">
			<tr>
				<td class="tbltop" colspan="5"><?php echo ($step == 3)?'Missing Service Info':'Missing Programs'; ?></td>
			</tr>
	<tr>
	<?php
		if ($step == 3) {
	?>
				<td><b>Service</b></td>
				<td align="center"><b>Init Script</B></td>
				<td align="center"><b>PID File</b></td>
	<?php
		} else {
	?>
				<td><b>Program</b></td>
				<td align="center"><b>Optional</b></td>
				<td align="center"><b>Required</b></td>
				<td align="center"><b>Located</b></td>
	<?php
		}
	?>
	</tr>
		<?php
		if ($step == 3) {
			$progs = $data['init'];
			$path = array($data['init_dir']);
		} else {
			$progs = $cfg['prog'];
			$path = array("/bin", "/usr/bin", "/usr/sbin");
		}
		$cnt = 0;
		foreach($progs as $key => $val) {
			$location = '';
			if (($curclass) == "row1") {
				$curclass = "row2";
			} else {
				$curclass = "row1";
			}
			if(strval($substep) == 'a')
				$location = $data['init'][$key];

			// Locate Programs that we need
			if (!file_exists($val) && $key != 'timeout' && !file_exists($cfg['init'][$key])) {
				$cnt++;
				$location = locate_program($key, $path);
				echo "<div id=\"$key\" class=\"popup\">";
				echo "<b>$key</b><br>";
				if($step == 3)
					echo "Requirements: <i><span class='small'>Required</span></i><br>";
				else
					echo "Requirements: <i><span class='small'>".(($cfg['prog-req'][$key]) ? "Required" : "Optional")."</span></i><br>";

				echo "Expected Location: <i><span class='small'>".$progs[$key]."</span></i><br>";
				if ($location != "") {
					echo "Located: <i><span class='small'>".$location."</span></i><br>";
				}
				if ($step == 3) {
					echo "Description: <i><span class='small'>The correct location must be found for all service init scripts</span></i></div>";

					echo "<tr class='$curclass'><td>".$key."</td><td>";
					echo "<input type='textbox' name='data[init--$key]' value='$location'></td><td>";
					echo "<input type='textbox' name='data[spid--$key]' value='".$cfg['spid'][$key]."'></td>";
					echo "<td class='row2'><img src='/icon/question.png' width='24' height='24' onmouseover=\"popup(event,'$key')\" onmouseout=\"popup(event,'$key')\"></td>";
					echo "</tr>";
				} else {
					echo "Description: <i><span class='small'>".$cfg['prog-desc'][$key]."</span></i></div>";
					if ($cfg['prog-req'][$key] == true) {
						if ($location != "") {
							echo "<input type='hidden' name='data[prog--$key]' value='$location'>";
						} else {
							$nosubmit = true;
						}
						echo "<tr class='$curclass'><td><span style='text-align: right;'>".(($location == "") ? "<font color=red>" : "").$key.(($location == "") ? "</font>" : "")."</span></td><td></td><td align=center><b>".(($location == "") ? "<font color=red>X</font>" : "X")."</b></td><td>";
						echo "<input type='textbox' name='data[prog--$key]' value='$location'></td>";
						echo "<td class='row2'><img src='/icon/question.png' width='24' height='24' onmouseover=\"popup(event,'$key')\" onmouseout=\"popup(event,'$key')\"></td>";
						echo "</tr>";
					} else {
						if ($location != "") {
							echo "<input type='hidden' name='data[prog--$key]' value='$location'>";
						}
						echo "<tr class='$curclass'><td style='text-align: right;'>".$key."</td><td align=center>X</td><td></td><td>";
						echo "<input type='textbox' name='data[prog--$key]' value='$location'></td>";
						echo "<td class='row2'><img src='/icon/question.png' width='24' height='24' onmouseover=\"popup(event,'$key')\" onmouseout=\"popup(event,'$key')\"></td>";
						echo "</tr>";
					}
				}
			}
		}
		if ($cnt == 0) {
			echo '<tr><td colspan=3>None</td></tr>';
		}
		?>

</table>
</td></tr>
</table><br>
<?php
if($step == 3) {
	if ($nosubmit == true) { ?>
		<input type="hidden" name="data[initscriptslocated]" value="false">
<?	} else { ?>
		<input type="hidden" name="data[initscriptslocated]" value="true">
<?	}
} else {
	if ($nosubmit == true) { ?>
			<input type="hidden" name="data[programslocated]" value="false">
<?	} else { ?>
			<input type="hidden" name="data[programslocated]" value="true">
<?	}
}
?>
